/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.process;

import org.openaecfm.model.actor.Person;
import org.openaecfm.model.kernel.Control;
import org.openaecfm.model.process.WorkControl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.EntityInstanceNameParameter;
import org.openaecfm.spf.model.ListParameter;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.SimpleEntityInstance;
import org.openaecfm.spf.model.StringParameter;

public final class WorkControlIO implements
		IEntityInstanceUtilInput<WorkControl>,
		IEntityInstanceUtilOutput<WorkControl> {
	private IEntityInstanceUtilInput<Control> utilInput;
	private IEntityInstanceUtilOutput<Control> utilOutput;

	public void setUtilOutput(IEntityInstanceUtilOutput<Control> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void exportSPF(WorkControl pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		utilOutput.exportSPF(pojo, ei, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set CreationDate
		sei.addParameter(new StringParameter(pojo.getCreationDate(),
				StringParameter.UTF_8));

		// Set Creators
		if (pojo.getCreators().isEmpty()) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			ListParameter l = new ListParameter();
			for (Person p : pojo.getCreators()) {
				l.add(new EntityInstanceNameParameter(appDataManager
						.getName(p)));
			}
			sei.addParameter(l);
		}

		// Set Purpose
		if (pojo.getPurpose() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getPurpose(),
					StringParameter.UTF_8));
		}

		// Set Duration
		if (pojo.getDuration() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getDuration(),
					StringParameter.UTF_8));
		}

		// Set TotalFloat
		if (pojo.getTotalFloat() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getTotalFloat(),
					StringParameter.UTF_8));
		}

		// Set StartTime
		sei.addParameter(new StringParameter(pojo.getStartTime(),
				StringParameter.UTF_8));

		// Set FinishTime
		if (pojo.getFinishTime() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new StringParameter(pojo.getFinishTime(),
					StringParameter.UTF_8));
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveNotReferenceParameter(EntityInstance ei, WorkControl pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set CreationDate
		StringParameter cd = (StringParameter) sei.getParameter(6);
		pojo.setCreationDate(cd.toUTF8());

		// Set Purpose
		Parameter purpose = sei.getParameter(8);
		if (purpose.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) purpose;
			pojo.setPurpose(s.toUTF8());
		}

		// Set Duration
		Parameter duration = sei.getParameter(9);
		if (duration.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) duration;
			pojo.setDuration(s.toUTF8());
		}

		// Set TotalFloat
		Parameter tf = sei.getParameter(9);
		if (tf.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) tf;
			pojo.setTotalFloat(s.toUTF8());
		}

		// Set StartTime
		StringParameter st = (StringParameter) sei.getParameter(10);
		pojo.setStartTime(st.toUTF8());

		// Set FinishTime
		Parameter ft = sei.getParameter(11);
		if (ft.getNodeType() == NodeType.STRING) {
			StringParameter s = (StringParameter) ft;
			pojo.setFinishTime(s.toUTF8());
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei, WorkControl pojo,
			IAppDataManager appDataManager) {
		utilInput.resolveReferenceParameter(ei, pojo, appDataManager);

		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set Creators
		Parameter creators = sei.getParameter(7);
		if (creators.getNodeType() == NodeType.LIST) {
			ListParameter l = (ListParameter) creators;
			for (Parameter p : l) {
				EntityInstanceNameParameter n = (EntityInstanceNameParameter) p;
				pojo.getCreators().add(
						(Person) appDataManager.getObject(n.toInstanceName()));
			}
		}
	}

	public void setUtilInput(IEntityInstanceUtilInput<Control> utilInput) {
		this.utilInput = utilInput;
	}
}
